package com.znufe.exam.service.excel;

import com.alibaba.excel.util.StringUtils;
import com.znufe.exam.utility.easyexcel.ExcelCheckErrDto;
import com.znufe.exam.utility.easyexcel.ExcelCheckResult;
import com.znufe.exam.viewmodel.admin.question.excelmodel.ShortAnswerFromExcelVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ShortAnswerServiceImpl implements ShortAnswerService {

    @Autowired
    public ShortAnswerServiceImpl() {}

    @Override
    public ExcelCheckResult checkImportExcel(List<ShortAnswerFromExcelVM> shortAnswerList, Integer questionType) {
        //成功结果集
        List<ShortAnswerFromExcelVM> successList = new ArrayList<>();
        //错误数组
        List<ExcelCheckErrDto<ShortAnswerFromExcelVM>> errList = new ArrayList<>();

        for(ShortAnswerFromExcelVM shortAnswer : shortAnswerList) {
            // 错误信息
            StringBuilder errMsg = new StringBuilder();
            if (null == shortAnswer.getTitle()) {
                errMsg.append("简答题题干不能为空！请修改").append(";");
            }
            if (null == shortAnswer.getCorrect()) {
                errMsg.append("答案不能为空，请为简答题设置答案！").append(";");
            }
            if (null == shortAnswer.getScore()) {
                errMsg.append("分数不能为空！请为题目设置分数！").append(";");
            }
            // 依据上面合法性判断的结果，
            // 正确的对象信息添加进successList，错误的对象信息添加进errList
            if (StringUtils.isEmpty(errMsg.toString())) {
                // 这里有两个选择，1、一个返回成功的对象信息，2、进行持久化操作
                System.out.println("ShortAnswerServiceImpl的 checkImportExcel里的 ：" + shortAnswer);
                successList.add(shortAnswer);
            } else {
                // 添加错误信息
                errList.add(new ExcelCheckErrDto(shortAnswer, errMsg.toString()));
            }
        }
        return new ExcelCheckResult(successList, errList);
    }
}
